//
//  MNDIntegralDetailViewController.m
//  minidai
//
//  Created by 谭谭伟 on 17/5/16.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDIntegralDetailViewController.h"
#import "MNDIntegralDetaiCell.h"
#import "MNDHistoryScoreModel.h"
#import "SYRefreshControl.h"
@interface MNDIntegralDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
//模型数组
@property (nonatomic, strong) NSMutableArray<MNDHistoryScoreModel*> *historyModelArray;
@property (weak, nonatomic) IBOutlet UITableView *historyScoreTableView;
//数据请求页码
@property(assign,nonatomic) NSInteger pageNum;

@end

@implementation MNDIntegralDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData:YES];
    self.historyScoreTableView.tableFooterView = [[UIView alloc]init];
    self.historyScoreTableView.estimatedRowHeight = 44;
    self.historyScoreTableView.rowHeight = UITableViewAutomaticDimension;
    // Do any additional setup after loading the view.
}


#pragma mark -懒加载
- (NSMutableArray<MNDHistoryScoreModel *> *)historyModelArray {
    if (!_historyModelArray) {
        _historyModelArray = [[NSMutableArray alloc]init];
    }
    return _historyModelArray;
}


#pragma mark -加载数据
- (void)loadData:(BOOL)isPullDown {
    
    if (self.historyModelArray.count <= 0) {
        [self.view beginLoading];
    }
     __weak typeof(self) weakSelf = self;

    if (isPullDown) {
        _pageNum = 1;
        
    }else {
        _pageNum ++;
    }
    
    MNDLoginUserModel *user = [MNDLoginUserModel currentLoginUser];
    [APIManager sendRequestWithPath:API_HISTORY_LIST_SCORE parameters:[MNDSign signedParamWithDict:@{@"user_id": user.user_id, @"per_page": @(_pageNum), @"pagesize": @10}] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        
        if (responseModel.status == ResponseSuccess) {
            NSArray *historyScoreArray = [MNDHistoryScoreModel mj_objectArrayWithKeyValuesArray:responseModel.data[@"action_list"]];
            if (isPullDown) {
                [weakSelf.view configBlankPage:SYBlankPageTypeNoData hasData:historyScoreArray.count reloadButtonBlock:nil];
                [weakSelf.historyModelArray removeAllObjects];
                
            }
            [SYRefreshControl configAfterLoading:weakSelf.historyScoreTableView hasData:historyScoreArray.count];
            [weakSelf.historyModelArray addObjectsFromArray:historyScoreArray];
        } else {
            [SYRefreshControl configAfterLoading:weakSelf.historyScoreTableView hasData:YES];
            [weakSelf.view configBlankPage:SYBlankPageTypeError hasData:NO reloadButtonBlock:^(id sender) {
                [weakSelf loadData:YES];
            }];
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
        }
        [weakSelf.historyScoreTableView reloadData];
        
    } onFailure:^(NSError *error) {
        [SYRefreshControl configAfterLoading:weakSelf.historyScoreTableView hasData:YES];
        if (isPullDown) {
            SYBlankPageType blankType = [weakSelf.view blankPageTypeWithError:error];
            [weakSelf.view configBlankPage:blankType hasData:weakSelf.historyModelArray.count reloadButtonBlock:^(id sender) {
                [weakSelf loadData:isPullDown];
            }];
        } else {
            [NetworkProgressHUD showHudNetworkError:error];
        }
    }];
}

#pragma mark -搭建页面

- (void)setupUI {
    WeakSelf(self);
    [SYRefreshControl configGifMJRefresh:self.historyScoreTableView refreshType:MJRefreshSupportALl beginRefresh:YES timeLabelHidden:YES stateLabelHidden:YES refreshBlock:^{
        [weakself loadData:YES];
    } loadMoreBlock:^{
        [weakself loadData:NO];
    }];
}

#pragma mark -UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.historyModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MNDHistoryScoreModel *model = self.historyModelArray[indexPath.row];
    MNDIntegralDetaiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MNDIntegralDetaiCell"];
    cell.actionMarksLabel.text = model.action_marks;
    if (model.action_num.integerValue > 0) {
        cell.actionNumLabel.text = [NSString stringWithFormat:@"+%@",model.action_num];
    }else {
        cell.actionNumLabel.text = [NSString stringWithFormat:@"%@",model.action_num];
    }
    
    cell.actionTimeLabel.text = model.action_time;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
